<template>
  <el-dialog :visible.sync="visible"
             :title="$t('updatePassword.title')"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :append-to-body="true">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item :label="$t('updatePassword.username')">
        <span>{{ $store.state.user.name }}</span>
      </el-form-item>
      <el-form-item prop="oldPassword"
                    :label="$t('updatePassword.password')">
        <el-input v-model.trim="dataForm.oldPassword"
                  type="password"
                  :placeholder="$t('updatePassword.password')"></el-input>
      </el-form-item>
      <el-form-item prop="newPassword"
                    :label="$t('updatePassword.newPassword')">
        <el-input v-model.trim="dataForm.newPassword"
                  type="password"
                  :placeholder="$t('updatePassword.newPassword')"></el-input>
      </el-form-item>
      <el-form-item prop="confirm"
                    :label="$t('updatePassword.comfirmPassword')">
        <el-input v-model.trim="dataForm.confirm"
                  type="password"
                  :placeholder="$t('updatePassword.comfirmPassword')"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button type="primary"
                 @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { clearLoginInfo } from '@/utils'
import Qs from 'qs'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        oldPassword: '',
        newPassword: '',
        confirm: ''
      }
    }
  },
  computed: {
    dataRule () {
      let validateComfirmPassword = (rule, value, callback) => {
        if (this.dataForm.newPassword !== value) {
          return callback(new Error(this.$t('updatePassword.validate.comfirmPassword')))
        }
        callback()
      }

      let validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else if (value.length > 16) {
          callback(new Error('请输入8-16位,包括大写字母,小写字母,数字'))
        } else {
          let pPattern = /^.*(?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,16}/
          // let order = 0 // 顺序
          if (pPattern.test(value)) {
            callback()
          } else {
            callback(new Error('请输入8-16位,包括大写字母,小写字母,数字'))
          }
        }
      }
      return {
        oldPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' }
        ],
        newPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' },
          { validator: validatePassword, trigger: 'change' }
        ],
        confirm: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' },
          { validator: validateComfirmPassword, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http.put('/passwordChange', Qs.stringify(this.dataForm)).then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                clearLoginInfo()
                this.$router.replace({ name: 'login' })
              }
            })
          }).catch(() => { })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
